package com.example.customviewtest.vertical_viewpager

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager

/**
 * @auther XuDan on 2020/7/22.
 */
class VerticalViewPager : ViewPager {
    constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    override fun onTouchEvent(ev: MotionEvent?): Boolean {
        return super.onTouchEvent(swapTouchEvent(MotionEvent.obtain(ev)))
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return super.onInterceptTouchEvent(swapTouchEvent(MotionEvent.obtain(ev)))
    }

    private fun swapTouchEvent(event: MotionEvent): MotionEvent? {
        val w: Float = width.toFloat()
        val h: Float = height.toFloat()
        event.setLocation((event.y / h) * w, (event.x / w) * h)
        return event
    }
}